<?php
defined('_JEXEC') or die;


class DoskaModelMessage extends JModelItem {
	
	
	protected function populateState() {
		$app = JFactory::getApplication();
		
		$id = $app->input->getInt('id');
		$this->setState('message.id',$id);
		
		$params = $app->getParams();
		$this->setState('params',$params);
	}
	
	public function getItem($pk = NULL) {
		$pk = (!empty($pk)) ? $pk : $this->getState('message.id');
		
		if($pk) {
			
			$db = $this->getDbo();
			$query = $db->getQuery(TRUE);
			
			$query->select('p.id, p.params, p.id_categories,p.id_types, title, introtext, `fulltext`, id_user, town, images, price, created, publish_up, publish_down, hits, metadesc, metakey, modified, p.alias');
			$query->from('#__doska_post AS p');
			$query->select('c.name as category, c.alias AS catalias');
			$query->join('LEFT','#__doska_categories AS c ON c.id=p.id_categories');
			$query->select('t.name as type, t.alias AS typealias');
		$query->join('LEFT','#__doska_types AS t ON t.id=p.id_types');
		
		$query->select('u.name AS author_name')
			->join('LEFT', '#__users AS u ON u.id=p.id_user');
			
		
		$query->where('p.id = '.$pk);
		$query->where('p.state = 1');
		$query->where('p.confirm = "1"');
		$query->where('p.publish_up <= NOW()');
		$query->where('p.publish_DOWN >= NOW()');
		
		
		$db->setQuery($query);
		
		$data = $db->loadObject();
		
		
		if($data) {
			$params = new JRegistry;
			$params->loadString($data->params);
			$data->params = $params;
			
			$params = clone $this->getState('params');
			$params->merge($data->params);
			
			$data->params = $params;
			
			return $data;
			
		}
		
			
		}
		else {
			return FALSE;
		}
	}
	
	protected function getStoreId($id = '')
	{
		
		$id .= ':' . $this->getState('message.id');
		$id .= ':' . $this->getState('params');
		
		//
		
		return parent::getStoreId($id);
	}
	
	public function hit($pk = 0) {
		
		$pk = (!empty($pk)) ? $pk : (int)$this->getState('message.id');
		
		$table = JTable::getInstance('Message','DoskaTable');
		
		if($table->load($pk)) {
			$table->hit($pk);
		}
	}
}